package com.linko.intelligent.common.exception;

import com.linko.intelligent.base.CommonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @Autowired
    private Environment environment;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object defaulErrorHandler(Exception e) {
        log.error("handle error:", e);
        String act = environment.getProperty("spring.profiles.active");
        //如果是开发环境，直接返回报错新信息，便于排除问题
        if (act.indexOf("dev") > -1 || act.indexOf("test") > -1) {
            return CommonResponse.constructResponse(0, e.getMessage());
        }
        return CommonResponse.constructResponse(0, "服务器繁忙");
    }
}
